<?php
/**
 * Author: Traveler
 * Time  : 2021/9/2 11:57
 */

namespace app\service;


use app\ErrorCode;
use app\model\IspModel;
use sunshine\basic\RestService;
use sunshine\exceptions\ApiException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class IspService extends RestService
{
    /**
     * IspService constructor.
     */
    public function __construct()
    {
        $this->model = new IspModel();
        $this->initialize();
    }

    /**
     * 初始化
     */
    public function initialize()
    {
    }

    /**
     * 删除运营商(重写)
     *
     * @param int        $id
     * @param array|null $relation
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function delete(int $id, ?array $relation = null): bool
    {
        // 检查运营商是否在使用中
        $icafeWanService  = new IcafeWanService();
        $icafeAdslService = new IcafeAdslService();
        if ($icafeWanService->getCountByIsp($id) > 0 || $icafeAdslService->getCountByIsp($id) > 0) {
            throw new ApiException(ErrorCode::ISP_USING);
        }

        return parent::delete($id);
    }


}